package tr.edu.bilgi.videocs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OptionalDataException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import tr.edu.bilgi.videocs.models.Video;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Custom adapter
 * 
 */
public class VideoListAdapter extends ArrayAdapter<String> {

	ArrayList<Video> videos;
	private Map<String, Drawable> drawableMap;
	private LayoutInflater mInflater;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public VideoListAdapter(Context context, int textViewResourceId,
			ArrayList<Video> videos) throws OptionalDataException,
			ClassNotFoundException, IOException {
		super(context, textViewResourceId, (List) videos);
		mInflater = LayoutInflater.from(context);
		this.videos = videos;
		drawableMap = new HashMap();
	}

	static class ViewHolder {
		ImageView thumb;
		TextView title;
		TextView speaker;
		TextView series;
	}


	@Override
	public View getView(final int i, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		convertView = mInflater.inflate(R.layout.videorow, null);
		holder = new ViewHolder();
		holder.thumb = (ImageView) convertView.findViewById(R.id.thumb);
		holder.title = (TextView) convertView.findViewById(R.id.title);
		holder.speaker = (TextView) convertView.findViewById(R.id.speaker);
		holder.series = (TextView) convertView.findViewById(R.id.series);
		convertView.setTag(holder);

		holder.thumb.setImageResource(R.drawable.loading);
		
		if (videos.get(i) != null) {
		  Log.d("VIDEOLIST", (Video) videos.get(i) + "");
		  Log.d("VIDEOLIST", videos.get(i).getTitle().toString());
			holder.title.setText(videos.get(i).getTitle());
			holder.speaker.setText(videos.get(i).getSpeaker().getName());
			if (videos.get(i).getSeries() != null)
				holder.series.setText(videos.get(i).getSeries().getTitle());
			else
				holder.series.setText("");

//			holder.thumb.setTag(videos.get(i).getThumbnailUrl() + ";"
//					+ videos.get(i).getId());
		//	new DownloadImageTask().execute(holder);
		}

		return convertView;
	}

	public Drawable fetchDrawable(String urlString, int id)
			throws MalformedURLException, IOException {
		if (drawableMap.containsKey(urlString)) {
			return drawableMap.get(urlString);
		}
		drawableMap.put(urlString,
				Drawable.createFromStream(fetch(urlString), "src"));
		return drawableMap.get(urlString);
	}

	private InputStream fetch(String urlString) throws MalformedURLException,
			IOException {
		return new DefaultHttpClient().execute(new HttpGet(urlString))
				.getEntity().getContent();
	}

	private class DownloadImageTask extends
			AsyncTask<ViewHolder, Void, ViewHolder> {
		Drawable drawable;
		String tagid;

		@Override
		protected ViewHolder doInBackground(ViewHolder... params) {
			tagid = (String) params[0].thumb.getTag();
			// Split the string on our delimiter ";" so we can pass the url and
			// id to fetchDrawable
			String[] test = tagid.split(";");
			try {
				drawable = fetchDrawable(test[0], Integer.parseInt(test[1]));
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println(e.getStackTrace());
				e.printStackTrace();
			}
			return params[0];
		}

		protected void onPostExecute(ViewHolder holder) {
			holder.thumb.setImageDrawable(drawable);
		}
	}
}